package com.wyh.service.impl;

import com.wyh.dao.InsuranceDao;
import com.wyh.entity.Insurance;
import com.wyh.entity.InsuranceType;
import com.wyh.entity.QueryVO;
import com.wyh.entity.User;
import com.wyh.service.InsuranceService;
import com.wyh.struct.PageResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author wangyaohan
 * @Date 2021/9/24 11:00
 * @ClassName InsuranceServiceImpl
 */
@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Resource
    private InsuranceDao insuranceDao;

    /**
     * 列表
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Override
    public PageResult find(Integer pageNum, Integer pageSize, QueryVO vo) {

        Specification<Insurance> specification = new Specification<Insurance>() {
            @Override
            public Predicate toPredicate(Root<Insurance> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                ArrayList<Predicate> predicates = new ArrayList<>();
                if (Objects.nonNull(vo)) {
                    //根据保险金额范围查询
                    if (Objects.nonNull(vo.getMaxNum()) && Objects.nonNull(vo.getMinNum())) {
                        predicates.add(cb.between(root.get("insuranceAmount").as(Double.class), vo.getMinNum(), vo.getMaxNum()));
                    }
                    //根据外键查询-》保险种类
                    if (Objects.nonNull(vo.getTid())) {
                        /*InsuranceType insuranceType = new InsuranceType();
                        insuranceType.setId(vo.getTid());
                        predicates.add(cb.equal(root.get("insuranceType").as(InsuranceType.class), insuranceType));
                        */
                        Join<InsuranceType, Insurance> join = root.join("insuranceType");
                        predicates.add(cb.equal(join.get("id").as(InsuranceType.class), vo.getTid()));
                    }
                    //根据附表字段查询-》投保人姓名、受益人姓名
                    if (Objects.nonNull(vo.getTname())) {
                        Join<User, Insurance> join = root.join("iuser", JoinType.LEFT);
                        predicates.add(cb.like(join.get("userName"), "%" + vo.getTname() + "%"));
                    }
                    if (Objects.nonNull(vo.getSname())) {
                        Join<User, Insurance> join = root.join("buser", JoinType.LEFT);
                        predicates.add(cb.like(join.get("userName"), "%" + vo.getSname() + "%"));
                    }
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Insurance> all = insuranceDao.findAll(specification, pageable);
        return new PageResult(all.getContent(), all.getTotalElements(), all.getTotalPages());
    }
}
